﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Touch2Like.Model;
using System.Data;

namespace Touch2Like.DAL
{
    public class OrderDAL
    {
        #region Add
        public int Add(OrderInfo info)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeOutParam("@ListID",SqlDbType.Int,4),
                db.MakeInParam("@UserGUID",SqlDbType.UniqueIdentifier,16, info.UserGUID),
                db.MakeInParam("@OrderNumber",SqlDbType.VarChar,200,info.OrderNumber),
                db.MakeInParam("@TotalPrice",SqlDbType.Decimal,9,info.TotalPrice),
                db.MakeInParam("@OrderStatus",SqlDbType.Int,4,info.OrderStatus)
            };
            try
            {
                db.ExecNonQuery("CAM_Order_Add", param);
                return DataHelper.GetInt(param[0].Value).Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion

        #region Update
        public void Update(OrderInfo info)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeInParam("@ListID",SqlDbType.Int,4,info.ListID),
                db.MakeInParam("@UserGUID",SqlDbType.UniqueIdentifier,16, info.UserGUID),
                db.MakeInParam("@OrderNumber",SqlDbType.VarChar,200,info.OrderNumber),
                db.MakeInParam("@TotalPrice",SqlDbType.Decimal,9,info.TotalPrice),
                db.MakeInParam("@OrderStatus",SqlDbType.Int,4,info.OrderStatus)
            };
            try
            {
                db.ExecNonQuery("CAM_Order_Update", param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion

        #region Get
        public OrderInfo GetByID(int listID)
        {
            Database db = new Database();
            SqlDataReader dr = null;
            SqlParameter[] param =
                {
                   db.MakeInParam("@ListID",SqlDbType.Int,4, listID)
                };

            try
            {
                dr = db.RunProc("CAM_Order_GetByID", param);

                if (dr.Read())
                {
                    OrderInfo info = new OrderInfo();

                    info.ListID = DataHelper.GetInt(dr["ListID"]);
                    info.UserGUID = DataHelper.GetString(dr["UserGUID"]);
                    info.OrderNumber = DataHelper.GetString(dr["OrderNumber"]);
                    info.TotalPrice = DataHelper.GetDecimal(dr["TotalPrice"]);
                    info.OrderStatus = DataHelper.GetInt(dr["OrderStatus"]);
                    info.CreateDate = DataHelper.GetDateTime(dr["CreateDate"]);
                    info.UpdateDate = DataHelper.GetDateTime(dr["UpdateDate"]);

                    return info;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
                db.Close();
            }
        }
        #endregion

        #region Get By OrderNumber
        public OrderInfo GetByOrderNumber(string ordernumber)
        {
            Database db = new Database();
            SqlDataReader dr = null;
            SqlParameter[] param =
                {
                   db.MakeInParam("@OrderNumber",SqlDbType.VarChar,200, ordernumber)
                };

            try
            {
                dr = db.RunProc("CAM_Order_GetByOrderNumber", param);

                if (dr.Read())
                {
                    OrderInfo info = new OrderInfo();

                    info.ListID = DataHelper.GetInt(dr["ListID"]);
                    info.UserGUID = DataHelper.GetString(dr["UserGUID"]);
                    info.OrderNumber = DataHelper.GetString(dr["OrderNumber"]);
                    info.TotalPrice = DataHelper.GetDecimal(dr["TotalPrice"]);
                    info.OrderStatus = DataHelper.GetInt(dr["OrderStatus"]);
                    info.CreateDate = DataHelper.GetDateTime(dr["CreateDate"]);
                    info.UpdateDate = DataHelper.GetDateTime(dr["UpdateDate"]);

                    return info;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
                db.Close();
            }
        }
        #endregion

        #region Get By UserID
        public DataTable GetByUserID(string UserGUID)
        {
            Database db = new Database();

            try
            {
                SqlParameter[] param =
                {
                    db.MakeInParam("@UserGUID", SqlDbType.UniqueIdentifier, 16, UserGUID)
                };
                return db.GetDataSet("CAM_Order_GetByUserID", param).Tables[0];
            }

            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion

        #region Delete
        public void Delete(int listID)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeInParam("@ListID",SqlDbType.Int,4,listID)
            };
            try
            {
                db.ExecNonQuery("CAM_Order_Delete", param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion
    }
}
